package com.microservice.salmon.oauth.handler;

import com.salmon.common.ApiResponse;
import com.salmon.common.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 统一异常处理
 * </p>
 *
 * @author 王洪庆 2019-01-03 09:34
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<ApiResponse> handleSystemException(Exception ex, HttpServletRequest request) {
        HttpStatus status = WebUtils.getStatus(new ServletWebRequest(request));
        ApiResponse apiResponse = ApiResponse.ofMessage(status.value(), ex.getMessage());
        return new ResponseEntity<>(apiResponse, status);
    }
}
